﻿using MyFeiFei.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyFeiFei
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        public void Init()
        {

        }
        public async Task Save()
        {
            await Task.Delay(1000);
            Bitmap image = new Bitmap((int)this.Width, (int)this.Height);
            Graphics g = Graphics.FromImage(image);

            g.CopyFromScreen((int)this.Left, (int)this.Top, 0, 0,
            new System.Drawing.Size((int)this.Width, (int)this.Height),
            CopyPixelOperation.SourceCopy);
            g.Dispose();

            image.Save("D://feifei.png");//默认保存格式为PNG，保存成jpg格式质量不是很好}
        }

        public void OpenSocket()
        {
            SocketTest socket = new SocketTest();
            socket.TargetSettings += Socket_TargetSettings;
            socket.OpenSocket();
        }

        private void Socket_TargetSettings(object sender, TargetSettingsEventArgs e)
        {
            var   msg = e.Msgs;
            this.Dispatcher.Invoke(new outputDelegate(outputAction), msg);
        }
        private delegate void outputDelegate(Msg msg);

        private void outputAction(Msg msg)
        {
            int other = 0;
            foreach (var item in msg.Data)
            {
                switch (item.GigaInferno)
                {
                    case ("14"):
                        Giga14.Text = item.Nub.ToString();
                        break;
                    case ("13"):
                        Giga13.Text = item.Nub.ToString();
                        break;
                    case ("12"):
                        Giga12.Text = item.Nub.ToString();
                        break;
                    case ("11"):
                        Giga11.Text = item.Nub.ToString();
                        break;
                    case ("10"):
                        Giga10.Text = item.Nub.ToString();
                        break;
                    case ("9"):
                        Giga9.Text = item.Nub.ToString();
                        break;
                    case ("8"):
                        Giga8.Text = item.Nub.ToString();
                        break;
                    default:
                        other += item.Nub;
                        Gigas.Text = other.ToString();
                        break;
                }
            }
            
            Save();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            HostVisual hostVisual = new HostVisual();
            Thread thread = new Thread(new ThreadStart(() =>
            {
                OpenSocket();
                System.Windows.Threading.Dispatcher.Run();
            }));

            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
        }
    }
}
